package com.zhang.springcloud.service.impl;

import com.zhang.springcloud.dao.OrderDao;
import com.zhang.springcloud.domain.Order;
import com.zhang.springcloud.service.AccountService;
import com.zhang.springcloud.service.OrderService;
import com.zhang.springcloud.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    OrderDao orderDao;
    @Resource
    AccountService accountService;
    @Resource
    StorageService storageService;
    @Override
    @GlobalTransactional(name = "fsp_order",rollbackFor = Exception.class)
    public void create(Order order) {
        System.out.println("产生订单");
        orderDao.create(order);
        System.out.println("库存扣减开始");
        storageService.decrease(order.getProduct_id(),order.getCount());
        System.out.println("库存扣减结束");
        System.out.println("账户扣减开始");
        accountService.decrease(order.getUser_id(),order.getMoney());
        System.out.println("账户扣减完毕");
        System.out.println("修改订单状态");
        orderDao.update(order.getUser_id(),0);
        System.out.println("结束");
    }
}
